var config = $.ajax({url: baseURL + 'sys/dict/init',async:false}).responseJSON;
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'aic/member/list',
        datatype: "json",
        colModel: [			
			{ label: '操作', name: 'id', index: 'id', width: 100, key: true,
				formatter: function(value, options, row) {
					var retVal = '<a href="javascript:balanceDetail(' + value + ')" class="label label-primary">资金明细</a>&nbsp;';
					retVal += '<a href="javascript:team(' + value + ')" style="display: inline-block;" class="label label-primary">团队</a>';
					return retVal;
				}
			},
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '真实姓名', name: 'realname', index: 'realname', width: 80 },
			{ label: '我的上级', name: 'recPerson', index: 'recPerson', width: 80 },
			{ label: '矿工等级 ', name: 'userLevel', index: 'user_level', width: 80,
				formatter: function(value, options, row) {
					for (var index in config.userLevel) {
						if (config.userLevel[index].code == value)
							return config.userLevel[index].value;
					}
					return value;
				}
			},
			{ label: '币数量┃冻结', name: 'balance', index: 'balance', width: 80,
				formatter: function(value, options, row) {
					return value + "┃" + row.blockedBalance;
				}
			},
			{ label: '注册IP', name: 'regIp', index: 'reg_ip', width: 90,
				formatter: function(value, options, row) {
					return "<a target='_blank' class='label label-info' href='https://www.baidu.com/s?wd=" + value + "'>"+value+"</a>";
				}
			},
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 110 },
			{ label: '认证状态', name: 'authStatus', index: 'auth_status', width: 65,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-primary">未认证</span>';
					} else if (value == 1) {
						return '<span class="label label-info">待审核</span>';
					} else if (value == 2) {
						return '<span class="label label-danger">未通过</span>';
					} else if (value == 3) {
						return '<span class="label label-success">已认证</span>';
					}
				}
			},
			{ label: '账号状态', name: 'status', index: 'status', width: 65,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-danger">禁用</span>';
					} else if (value == 1) {
						return '<span class="label label-success">正常</span>';
					} else if (value == 2) {
						return '<span class="label label-warning">冻结</span>';
					}
				}
			}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

    //文件上传
    $(".upload").each(function(i,el){
		new AjaxUpload('#' + $(el).attr("id") , {
		    action: baseURL + "sys/oss/upload",
		    name: 'file',
		    autoSubmit:true,
		    responseType:"json",
		    onSubmit:function(file, extension){
		        if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
		            alert('只支持jpg、png、gif格式的图片！');
		            return false;
		        }
		    },
		    onComplete : function(file, r){
		        if(r.code == 0){
		        	vm.member[$(el).attr("id")] = r.url;
		        	alert("上传成功");
		        }else{
		            alert(r.msg);
		        }
		    }
		});
    });
});

function balanceDetail(id){
	layer.open({
		  type: 1,
	      title: '资金明细',
	      area: ['700px', '500px'],
	      content: '<table id="jqGrid1""></table><div id="jqGridPager1"></div>',
	      success: function(){
	    	  $("#jqGrid1").jqGrid({
	    	        url: baseURL + "aic/balancedetail/list?memberId=" +  id,
	    	        datatype: "json",
	    	        colModel: [			
	    				{ label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true },
	    				{ label: '收支变化', name: 'balanceChange', index: 'balance_change', width: 80 }, 			
	    				{ label: '分类', name: 'category', index: 'category', width: 80,
	    					formatter: function(value, options, row) {
	    						if (value == 0) {
	    							return '买入';
	    						} else if (value == 1) {
	    							return '卖出';
	    						} else if (value == 2) {
	    							return '收益';
	    						} else if (value == 3) {
	    							return '奖励';
	    						}
	    						return value;
	    					}
	    				}, 			
	    				{ label: '时间', name: 'createTime', index: 'create_time', width: 80 }			
	    	        ],
	    			viewrecords: true,
	    	        height: 375,
	    	        rowNum: 10,
	    			rowList : [10,30,50],
	    	        rownumbers: true, 
	    	        rownumWidth: 25, 
	    	        autowidth:true,
	    	        pager: "#jqGridPager1",
	    	        jsonReader : {
	    	            root: "page.list",
	    	            page: "page.currPage",
	    	            total: "page.totalPage",
	    	            records: "page.totalCount"
	    	        },
	    	        prmNames : {
	    	            page:"page", 
	    	            rows:"limit", 
	    	            order: "order"
	    	        },
	    	        gridComplete:function(){
	    	        	//隐藏grid底部滚动条
	    	        	$("#jqGrid1").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
	    	        }
	    	    });
	      }
	});
}

function team(id){
	layer.open({
		  type: 1,
	      title: '团队',
	      area: ['700px', '500px'],
	      content: '<div class="tabbable" id="tabs-114497"><ul class="nav nav-tabs">' +
				  	'<li class="active"><a href="#panel-611577" data-toggle="tab">直推会员</a></li>' +
					'<li><a href="#panel-611578" data-toggle="tab" onclick=teamDetail(2,' + id + ')>二级会员</a></li>' +
					'<li><a href="#panel-611579" data-toggle="tab" onclick=teamDetail(3,' + id + ')>三级会员</a></li></ul>' +
					'<div class="tab-content">' +
					'<div class="tab-pane active" id="panel-611577"><p></p>' +
					'<table id="jqTable1""></table><div id="jqTablePager1"></div></div>' +
					'<div class="tab-pane" id="panel-611578"><p></p>' +
					'<table id="jqTable2""></table><div id="jqTablePager2"></div></div>' +
					'<div class="tab-pane" id="panel-611579"><p></p>' +
					'<table id="jqTable3""></table><div id="jqTablePager3"></div></div></div></div>',
	      success: function() {
	    	  teamDetail(1,id);
	      }
	});
}

function teamDetail(level,id) {
	var table = $("#jqTable" + level);
	if(table.getGridParam("reccount")) {
		return;
	}
	table.jqGrid({
        url: baseURL + "aic/team/list",
        datatype: "json",
        postData:{"level" : level, "id": id},
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true },
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 80 }, 			
			{ label: '真实姓名', name: 'realname', index: 'realname', width: 80 },
			{ label: '矿工等级 ', name: 'userLevel', index: 'user_level', width: 80,
				formatter: function(value, options, row) {
					for (var index in config.userLevel) {
						if (config.userLevel[index].code == value)
							return config.userLevel[index].value;
					}
				}
			},
			{ label: '币数量', name: 'balance', index: 'balance', width: 80 },
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 145 },
			{ label: '认证状态', name: 'authStatus', index: 'auth_status', width: 65,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-primary">未认证</span>';
					} else if (value == 1) {
						return '<span class="label label-info">待审核</span>';
					} else if (value == 2) {
						return '<span class="label label-danger">未通过</span>';
					} else if (value == 3) {
						return '<span class="label label-success">已认证</span>';
					}
				}
			},
			{ label: '账号状态', name: 'status', index: 'status', width: 65,
				formatter: function(value, options, row) {
					if (value == 0) {
						return '<span class="label label-danger">禁用</span>';
					} else if (value == 1) {
						return '<span class="label label-success">正常</span>';
					} else if (value == 2) {
						return '<span class="label label-warning">冻结</span>';
					}
				}
			}
        ],
		viewrecords: true,
        height: 305,
        rowNum: 5,
		rowList : [5,10,30],
        rownumbers: true, 
        rownumWidth: 25, 
        width: 698,
        pager: "#jqTablePager" + level,
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete : function() {
        	$("#jqTablePager" + level + "_center").width("352px");
        	//隐藏grid底部滚动条
        	table.closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{ mobile: null, authStatus: ""},
		showList: true,
		title: null,
		member: {},
		userLevel: config.userLevel,
		memberStatus: config.memberStatus,
		authStatus: config.authStatus,
		bank: config.bank,
		bankName: ""
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增会员";
			vm.member = {status: 1, authStatus: 0, userLevel: 0, bankName: vm.bank[0].value};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "会员详情";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.member.id == null ? "aic/member/save" : "aic/member/update";
			if(vm.bankName == "其它银行") {
				vm.member.bankName = vm.member.bankName1;
			} else {
				vm.member.bankName = vm.bankName;
			}
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.member),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(id){
			$.get(baseURL + "aic/member/info/"+id, function(r){
                vm.member = r.member;
                vm.bankName = vm.member.bankName;
                var isOtherBank = true;
                $.each(config.bank, function(index, item) {
                	if(vm.bankName == item.value) {
                		isOtherBank = false;
                		return false;
                	}
                });
                if(isOtherBank) {
                	vm.member.bankName1 = vm.member.bankName;
                	vm.bankName = "其它银行";
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:vm.q,
                page:page
            }).trigger("reloadGrid");
		},
		query: function () {
            vm.reload();
        },
        changeStatus: function (type) {
        	
        	var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			confirm('您确定操作？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "aic/member/changeStauts",
                    dataType: "json",
				    data: $.param({memberIds:ids,type:type},true),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
        }
	}
});